package org.jeecg.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.domain.Activity;
import org.jeecg.domain.Coupon;
import org.jeecg.domain.dto.AddDto;
import org.jeecg.domain.dto.FindDto;
import org.jeecg.service.ActivityService;
import org.jeecg.service.CouponService;
import org.jeecg.mapper.CouponMapper;
import org.jeecg.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author ASUS
* @description 针对表【coupon】的数据库操作Service实现
* @createDate 2024-12-26 08:36:32
*/
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon>
    implements CouponService{
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private ActivityService activityService;
    @Override
    public ResponseResult findAll(FindDto findDto) {
        Page<Coupon> page=new Page<>(findDto.getPageNum(),findDto.getPageSize());
        List<Coupon> list = couponMapper.findAll(page,findDto);
        for (Coupon coupon : list) {
            Activity byId = activityService.getById(coupon.getActivityId());
            if(byId!=null){
                coupon.setActivity(byId);
            }
        }
        page.setRecords(list);
        page.setTotal(list.size());
        return ResponseResult.okResult(page);
    }

    @Override
    public ResponseResult deleteOne(String id) {
        this.removeById(id);
        return ResponseResult.okResult(200);
    }

    @Override
    public ResponseResult addCoupon(AddDto addDto) {
        Coupon coupon=new Coupon();
        coupon.setUserId(0L);
        coupon.setUserName("0");
        coupon.setUserPhone("0");
        coupon.setValidityTime(new Date());
        coupon.setUseTime(new Date());
        coupon.setOrdersId("0");
        coupon.setCreateTime(new Date());
        coupon.setUpdateTime(new Date());
        coupon.setName(addDto.getName());
        coupon.setActivityId(addDto.getActivityId());
        coupon.setType(addDto.getType());
        coupon.setDiscountRate(addDto.getDiscountRate());
        coupon.setDiscountAmount(addDto.getDiscountAmount());
        coupon.setAmountCondition(addDto.getAmountCondition());
        coupon.setStatus(1);
        this.save(coupon);
        return ResponseResult.okResult(200);
    }

    @Override
    public ResponseResult updCoupon(Coupon coupon) {
        this.updateById(coupon);
        return ResponseResult.okResult(200);
    }
}




